package com.zgt.util;

import java.io.*;
import java.lang.reflect.Field;

import jxl.*;
import jxl.write.*;
import jxl.format.*;
import java.util.*;
import java.awt.Color;

import com.zgt.model.Teacher;

public class ToExcel {
	public static void writeExcel(File fileName, List list, Class className)
			throws Exception {
		WritableWorkbook wwb = Workbook.createWorkbook(fileName);
		WritableSheet ws = wwb.createSheet("Sheet1", 0);

		Field fields[] = className.getFields();
		Iterator it = list.iterator();
		int r = 0;
		while (it.hasNext()) {
			for (int i = 0; i < fields.length; i++) {
				Label c = new Label(i, r, "中国爱a");
				ws.addCell(c);
			}
			r++;
		}

		/*
		 * WritableFont wfc = new WritableFont(WritableFont.ARIAL, 20,
		 * WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
		 * jxl.format.Colour.GREEN); WritableCellFormat wcfFC = new
		 * WritableCellFormat(wfc); wcfFC.setBackground(jxl.format.Colour.RED);
		 * labelC = new Label(6, 0, "中国爱a", wcfFC); ws.addCell(labelC);
		 */
		// 写入Exel工作表
		wwb.write();
		// 关闭Excel工作薄对象
		wwb.close();
	}

	@SuppressWarnings("unchecked")
	public static List readExcel(File os) throws Exception {
		Workbook wb = Workbook.getWorkbook(os);// 找到文件
		Sheet s = wb.getSheet("Sheet1");// 文件中第几个表
		List list = new ArrayList();
		String temString;
		for (int i = 1; i < s.getRows(); i++) {
			for (int j = 0; j < s.getColumns(); j++) {
				temString = s.getCell(j, i).getContents();
				if(temString.trim().isEmpty()){
					break;
				}
				list.add(temString);
			}
		}
		wb.close();
		return list;
	}

	public static void readExcels(File os) throws Exception {
		Workbook wb = Workbook.getWorkbook(os);// 找到文件
		Sheet s = wb.getSheet("Sheet1");// 文件中第几个表
		System.out.println("row:" + s.getRows());
		System.out.println("col:" + s.getColumns());

		wb.close();
	}

}